<?php


namespace App\Repositories;


use App\Models\Customer as Model;
use App\Traits\CodeTrait;
use App\Traits\Logs;
use Illuminate\Support\Collection;
use LAuth\Models\User;
use LTools\Repositories\RepositoryAbstract;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;


class Customer extends RepositoryAbstract
{
    use CodeTrait, Logs;

    /**
     * Customer constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $columns
     * @return \Illuminate\Database\Eloquent\Collection
     * @author luffyzhao@vip.126.com
     */
    public function get(array $columns)
    {
        return $this->model->where('status', '=', 2)->get($columns);
    }

    /**
     * @param array $attributes
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        $attributes['code'] = $this->unique(4, 'C');
        /** @var Model $model */
        $model = parent::create($attributes);

        $model->docking()->create(
            Collection::make($attributes['docking'])->only(['callback'])->toArray()
        );

        $this->recordLogs($model, '客户创建成功');

        return $model;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->model->with(['docking'])->where('id', '=', $id)->first();
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update($id, array $values)
    {
        $model = $this->find($id);
        if (in_array($model->getAttribute('status'), [0, 4])) {
            $model->setAttribute('status', 0);
            $bool = $model->fill($values)->saveOrFail();

            if ($bool) {
                $this->recordLogs($model, '客户更新成功');
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是草稿或者审核驳回状态，不能编辑！');
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function show($id)
    {
        return $this->model->with(['logs.user'])->where('id', '=', $id)->firstOrFail();
    }

    /**
     * @param $id
     * @return bool|mixed
     * @throws \Exception
     * @author luffyzhao@vip.126.com
     */
    public function delete($id)
    {
        /** @var Model $model */
        $model = $this->find($id);
        if ($model->getAttribute('status') === 0) {
            $model->docking()->delete();
            return $model->delete();
        }
        throw new AccessDeniedHttpException('客户不是草稿状态，不能删除！');
    }

    /**
     * @param $id
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function submit($id)
    {
        $model = $this->model->newQuery()->where('id', '=', $id)->lockForUpdate()->first();
        if ($model->getAttribute('status') === 0) {
            $model->setAttribute('status', 1);
            $bool = $model->save();
            if ($bool) {
                $this->recordLogs($model, '客户确认成功');
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是草稿状态，不能确认！');
    }

    /**
     * @param $id
     * @param $note
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function reject($id, $note)
    {
        $model = $this->model->newQuery()->where('id', '=', $id)->lockForUpdate()->first();
        if ($model->getAttribute('status') === 1) {
            $model->setAttribute('status', 4);
            $bool = $model->save();
            if ($bool) {
                $this->recordLogs($model, '审核驳回:' . $note);
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是审核中状态，不能驳回！');
    }

    /**
     * @param $id
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function adopt($id)
    {
        $model = $this->model->newQuery()->where('id', '=', $id)->lockForUpdate()->first();
        if ($model->getAttribute('status') === 1) {
            $model->setAttribute('status', 2);
            $bool = $model->save();

            if ($bool) {
                $this->recordLogs($model, '客户审核成功');
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是审核中状态，不能审核成功！');
    }

    /**
     * @param $id
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function frozen($id, $value)
    {
        $model = $this->model->newQuery()->where('id', '=', $id)->lockForUpdate()->first();
        if ($model->getAttribute('status') === 2) {
            $model->setAttribute('status', 3);
            $bool = $model->save();
            if ($bool) {
                $this->recordLogs($model, '客户冻结成功：' . $value);
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是审核通过状态，不能冻结！');
    }

    /**
     * @param $id
     * @param $value
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function unfrozen($id, $value)
    {
        $model = $this->model->newQuery()->where('id', '=', $id)->lockForUpdate()->first();
        if ($model->getAttribute('status') === 3) {
            $model->setAttribute('status', 2);
            $bool = $model->save();
            if ($bool) {
                $this->recordLogs($model, '客户解冻成功：' . $value);
            }
            return $bool;
        }
        throw new AccessDeniedHttpException('客户不是冻结状态，不能解冻！');
    }

}
